package m202401.d12;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/12 09:45
 */
//114. 二叉树展开为链表
public class Q114 {
    public void flatten(TreeNode root) {
        List<TreeNode> list = new LinkedList<>();
        travel(root, list);
        TreeNode p = null;
        for (TreeNode node : list) {
            if (p == null) {
                p = node;
                p.left = null;
            } else {
                p.right = node;
                node.left = null;
                p = p.right;
            }
        }
    }

    private void travel(TreeNode node, List<TreeNode> list) {
        if (node == null) {
            return;
        }
        list.add(node);
        travel(node.left, list);
        travel(node.right, list);
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
